package push

import (
	"fmt"
	"github.com/ylywyn/jpush-api-go-client"
)

var (
	appKey string
	secret string
	//alert  string
)

func InitJpush(myAppKey, mySecret string) {
	appKey = myAppKey
	secret = mySecret
	//alert = myAlert
}

func PushNotice2Point(id, content, alert string, extras map[string]interface{}) error {
	//Platform
	var pf jpushclient.Platform
	pf.All()

	//Audience
	var ad jpushclient.Audience
	ad.SetAlias([]string{id})

	//Notice
	var notice jpushclient.Notice
	notice.SetAlert(alert)

	//	var msg jpushclient.Message
	//	msg.Content = content
	//	msg.Extras = extras

	payload := jpushclient.NewPushPayLoad()
	payload.SetPlatform(&pf)
	payload.SetAudience(&ad)
	//payload.SetMessage(&msg)
	payload.SetNotice(&notice)
	payload.SetOptions(&jpushclient.Option{TimeLive: 864000})

	bytes, _ := payload.ToBytes()

	//push
	c := jpushclient.NewPushClient(secret, appKey)
	_, err := c.Send(bytes)
	return err
}

func PushMsg2Point(ids []string, content string, extras map[string]interface{}) error {
	//Platform
	var pf jpushclient.Platform
	pf.All()

	//Audience
	var ad jpushclient.Audience
	ad.SetAlias(ids)

	//Notice
	//	var notice jpushclient.Notice
	//	notice.SetAlert(alert)

	var msg jpushclient.Message
	msg.Content = content
	msg.Extras = extras

	payload := jpushclient.NewPushPayLoad()
	payload.SetPlatform(&pf)
	payload.SetAudience(&ad)
	payload.SetMessage(&msg)
	//payload.SetNotice(&notice)
	payload.SetOptions(&jpushclient.Option{TimeLive: 864000})

	bytes, _ := payload.ToBytes()

	//push
	c := jpushclient.NewPushClient(secret, appKey)
	_, err := c.Send(bytes)
	return err
}

func PushNotice2All(content string, extras map[string]interface{}) error {
	//Platform
	var pf jpushclient.Platform
	pf.All()

	//Audience
	var ad jpushclient.Audience
	ad.All()

	//Notice
	//var notice jpushclient.Notice
	//notice.SetAlert(alert)

	var msg jpushclient.Message
	msg.Content = content
	msg.Extras = extras

	payload := jpushclient.NewPushPayLoad()
	payload.SetPlatform(&pf)
	payload.SetAudience(&ad)
	payload.SetMessage(&msg)
	//payload.SetNotice(&notice)
	payload.SetOptions(&jpushclient.Option{TimeLive: 864000})

	bytes, _ := payload.ToBytes()

	//push
	c := jpushclient.NewPushClient(secret, appKey)
	_, err := c.Send(bytes)
	return err
}

func Push2Group() error {
	return nil
}

func main1() {

	//Platform
	var pf jpushclient.Platform
	pf.All()

	//Audience
	var ad jpushclient.Audience
	s := []string{"1", "2", "3"}
	ad.SetTag(s)
	ad.SetAlias(s)
	ad.SetID(s)
	ad.All()

	//Notice
	var notice jpushclient.Notice
	notice.SetAlert("alert_test")
	notice.SetAndroidNotice(&jpushclient.AndroidNotice{Alert: "AndroidNotice"})
	notice.SetIOSNotice(&jpushclient.IOSNotice{Alert: "IOSNotice"})
	notice.SetWinPhoneNotice(&jpushclient.WinPhoneNotice{Alert: "WinPhoneNotice"})

	var msg jpushclient.Message
	msg.Title = "Hello"
	msg.Content = "你是ylywn"

	payload := jpushclient.NewPushPayLoad()
	payload.SetPlatform(&pf)
	payload.SetAudience(&ad)
	payload.SetMessage(&msg)
	payload.SetNotice(&notice)

	bytes, _ := payload.ToBytes()
	fmt.Printf("%s\r\n", string(bytes))

	//push
	c := jpushclient.NewPushClient(secret, appKey)
	str, err := c.Send(bytes)
	if err != nil {
		fmt.Printf("err:%s", err.Error())
	} else {
		fmt.Printf("ok:%s", str)
	}
}
